home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / pxbud.zip / PXGEN.CPP < prev    next >
C/C++ Source or Header  |  1991-05-17  |  9KB  |  264 lines

  1. /*┌───────────────────────────────────────────────────────────────────────┐
  2.   │                                                                       │
  3.   │   Module:  PXGEN.CPP                                                  │
  4.   │   Author:  Rick Kligman                                               │
  5.   │   Purpose: This program will generate the code for a table class      │
  6.   │                                                                       │
  7.   │   Last Modified: 05-17-91 00:34am                                     │
  8.   │                                                                       │
  9.   │   Version 1.00                                                        │
  10.   └───────────────────────────────────────────────────────────────────────┘ */
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. extern "C" {
  16.   #include <pxengine.h>
  17. }
  18.  
  19. TABLEHANDLE   pdxtbl_struct;
  20. RECORDHANDLE  pdxrec_struct;
  21. FILE          *cfile;
  22. FILE          *hfile;
  23.  
  24. char          tblnameslsh [80];         // table name w\full path and slashes
  25. char          tblname [80];             // table name with full path
  26. char          dbname  [10];             // table name w/o path
  27. char          cppfile [80];             // .cpp output file
  28. char          hppfile [80];             // .h output file
  29. char          outfile [10];             // output file w/o . extension
  30. char          fldnames [255] [26];      // converted fieldnames
  31. char          fldtypes [255];           // fieldtypes
  32. char          act_fldnames [255] [26];  // actual, untouched fieldnames
  33.  
  34. int           nflds;                    // number of fields in table
  35.  
  36. void    get_struct();
  37. void    public_h_funct();
  38. void    private_h_funct();
  39. void    convert_fieldname(char *, int);
  40. void    cpp_header(void);
  41. void    init_flds(void);
  42.  
  43.  
  44. //    ╓─────────────────────────────────────────────────────────────────╖
  45. //    ║                           Main function                         ║
  46. //    ╚═════════════════════════════════════════════════════════════════╝
  47.  
  48. void main(int argc, char **argv)
  49. {
  50.   int   i = 0, x = 0;
  51.  
  52.   if (argc != 4) {
  53.       printf("Usage: pxgen <pdxtable w/path> <8 char output file w/o extension (.cpp/.h)> <dbname>\n");
  54.       exit(1);
  55.   }
  56.  
  57.   // PXNetInit("f:\\pdx\\pdoxdata",NOVELLNET,NULL);  // change this line for net
  58.   PXInit();
  59.  
  60.   strcpy(tblname, argv[1]);
  61.   while ( *(tblname + x) != NULL ) {
  62.     if (*(tblname + x) == '\\') {
  63.         *(tblnameslsh + i)  = '\\';
  64.         i++;
  65.     }
  66.     *(tblnameslsh + i) = *(tblname + x);
  67.     i++;
  68.     x++;
  69.   }
  70.  
  71.   strcpy(outfile, argv[2]);
  72.   strcpy(cppfile, argv[2]);
  73.   strcat(cppfile, ".cpp");
  74.   strcpy(hppfile, argv[2]);
  75.   strcat(hppfile, ".h");
  76.   strcpy(dbname, argv[3]);
  77.  
  78.   get_struct();                   // get all the info for structure
  79.   public_h_funct();               // write public .h functions
  80.   private_h_funct();              // write private .h functions
  81.   cpp_header();
  82.   init_flds();
  83.  
  84.   fclose(hfile);
  85.   fclose(cfile);
  86.   PXExit();
  87.  
  88. }
  89.  
  90. //    ╓─────────────────────────────────────────────────────────────────╖
  91. //    ║                    Get Structure of a table                     ║
  92. //    ╚═════════════════════════════════════════════════════════════════╝
  93.  
  94. void get_struct()
  95. {
  96.   int i;
  97.   char fldname[26], fldtype[5];
  98.  
  99.   PXTblOpen(tblname, &pdxtbl_struct, 0, 1);
  100.   PXRecBufOpen(pdxtbl_struct, &pdxrec_struct);
  101.   PXRecNFlds(pdxtbl_struct, &nflds);
  102.  
  103.   cfile = fopen(cppfile, "w+");       // open up .cpp file to write to
  104.   hfile = fopen(hppfile, "w+");       // open up .h file to write to
  105.  
  106.   fprintf(hfile, "/**********************************************************\n");
  107.   fprintf(hfile, " *                                                        *\n");
  108.   fprintf(hfile, " * %s \n", hppfile);
  109.   fprintf(hfile, " * Header file for: %s \n", tblname);
  110.   fprintf(hfile, " *                                                        *\n");
  111.   fprintf(hfile, " **********************************************************/\n\n");
  112.  
  113.   fprintf(hfile, "#ifndef RKPDXTBL_H \n");
  114.   fprintf(hfile, "  #include \"pdxtbl.h\" \n");
  115.   fprintf(hfile, "#endif \n\n");
  116.  
  117.   fprintf(hfile, "class %s : public pxtable \n", dbname);
  118.   fprintf(hfile, "{ \n");
  119.  
  120.   fprintf(hfile, "public:\n\n");
  121.   for (i=1; i <= nflds; i++) {          /* get each field info    */
  122.       PXFldName(pdxtbl_struct, i, 26, fldname);
  123.       PXFldType(pdxtbl_struct, i,  5, fldtype);
  124.       strcpy(act_fldnames [i], fldname);
  125.       fldtypes [i] = *fldtype;
  126.  
  127.       convert_fieldname(fldname, i);    // convert fieldname to usable form
  128.  
  129.       switch ( *fldtype ) {
  130.         case 'A':
  131.             fprintf(hfile,"  CharFld   %s;\n", fldnames [i]);
  132.             break;
  133.         case 'S':
  134.             fprintf(hfile,"  ShortFld  %s;\n", fldnames [i]);
  135.             break;
  136.         case 'N':
  137.         case '$':
  138.             fprintf(hfile,"  DblFld    %s;\n", fldnames [i]);
  139.             break;
  140.         case 'D':
  141.             fprintf(hfile,"  DateFld   %s;\n", fldnames [i]);
  142.             break;
  143.       }
  144.    }
  145.  
  146.   fprintf(hfile, "\n");
  147.   PXTblClose(pdxtbl_struct);
  148. }
  149.  
  150. //    ╓──────────────────────────────────────────────────────────────╖
  151. //    ║                     Public .h functions                      ║
  152. //    ╚══════════════════════════════════════════════════════════════╝
  153.  
  154. void public_h_funct()
  155. {
  156.   fprintf(hfile, "  %s() \n", dbname);
  157.   fprintf(hfile, "      { strcpy(tblname, \"%s\"); }\n\n", tblnameslsh);
  158.  
  159.   fprintf(hfile, "  int   init_flds();\n");
  160. }
  161.  
  162. //    ╓──────────────────────────────────────────────────────────────╖
  163. //    ║                     Private .h functions                     ║
  164. //    ╚══════════════════════════════════════════════════════════════╝
  165.  
  166. void private_h_funct()
  167. {
  168.   fprintf(hfile, "\nprivate: \n\n");
  169.   fprintf(hfile, "};\n");
  170. }
  171.  
  172. //    ╓──────────────────────────────────────────────────────────────────╖
  173. //    ║   Convert fieldnames to lower case and special char checking     ║
  174. //    ╚══════════════════════════════════════════════════════════════════╝
  175.  
  176. void convert_fieldname(char *fldname, int i)
  177. {
  178.   int     x = 0, origlen;
  179.   char    *token;
  180.   char    temploc [26];
  181.  
  182.   strlwr(fldname);
  183.   strcpy(fldnames [i], fldname);
  184.   origlen = strlen(fldnames [i]);
  185.  
  186.   token = strtok(fldnames [i], " !@#$%^&*()+-=/'");
  187.   *temploc = NULL;
  188.  
  189.   while ( token != NULL ) {
  190.       if (x > 0)
  191.           strcat(temploc, "_");
  192.       x++;
  193.       strcat(temploc, token);
  194.       token = strtok(NULL, " !@#$%^&*()+-=/'");
  195.   }
  196.  
  197.   strcpy(fldnames [i], temploc);
  198.   if ( strlen(fldnames [i]) != origlen )
  199.       strcat(fldnames [i], "_");
  200.  
  201.   if ( strcmp(fldnames [i], "void") == 0 )
  202.       strcpy(fldnames [i], "avoid");
  203.  
  204.   if ( strcmp(fldnames [i], "class") == 0 )
  205.       strcpy(fldnames [i], "aclass");
  206.  
  207.   if ( strcmp(fldnames [i], "short") == 0 )
  208.       strcpy(fldnames [i], "ashort");
  209.  
  210.   if ( strcmp(fldnames [i], "double") == 0 )
  211.       strcpy(fldnames [i], "adouble");
  212.  
  213.   if ( strcmp(fldnames [i], "long") == 0 )
  214.       strcpy(fldnames [i], "along");
  215.  
  216.   if ( strcmp(fldnames [i], "char") == 0 )
  217.       strcpy(fldnames [i], "achar");
  218. }
  219.  
  220. //    ╓─────────────────────────────────────────────────────────────╖
  221. //    ║                 Write out .cpp file header                  ║
  222. //    ╚═════════════════════════════════════════════════════════════╝
  223.  
  224. void cpp_header()
  225. {
  226.   fprintf(cfile, "/**********************************************************\n");
  227.   fprintf(cfile, " *                                                        *\n");
  228.   fprintf(cfile, " * %s \n", cppfile);
  229.   fprintf(cfile, " * Header file for: %s \n", tblname);
  230.   fprintf(cfile, " *                                                        *\n");
  231.   fprintf(cfile, " **********************************************************/\n\n");
  232.  
  233.   fprintf(cfile, "#include \"%s\"\n", hppfile);
  234.   fprintf(cfile, "\n");
  235. }
  236.  
  237. //    ╓─────────────────────────────────────────────────────────────╖
  238. //    ║                     init_flds function                      ║
  239. //    ╚═════════════════════════════════════════════════════════════╝
  240.  
  241. void init_flds()
  242. {
  243.   int   i;
  244.  
  245.   fprintf(cfile, "int %s::init_flds() \n", dbname);
  246.   fprintf(cfile, "{ \n");
  247.   fprintf(cfile, "  int   i = 0; \n\n");
  248.  
  249.   for (i = 1; i <= nflds; i++) {
  250.       fprintf(cfile, "  fldptr [i++] = &%s;\n", fldnames [i]);
  251.   }
  252.  
  253.   fprintf(cfile, "  fldptr [i]   = NULL;\n\n");
  254.  
  255.   for (i = 1; i <= nflds; i++)
  256.       fprintf(cfile, "  %s.setinfo(*this, \"%s\");\n",
  257.                         fldnames [i], act_fldnames [i]);
  258.  
  259.   fprintf(cfile, "\n");
  260.   fprintf(cfile, "  return (PXSUCCESS);\n");
  261.   fprintf(cfile, "}\n\n");
  262. }
  263.  
  264.